import { Injectable } from '@nestjs/common';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { PrismaClient } from '@prisma/client';
import { toTrees } from '../../utils/toTree';

@Injectable()
export class MenuService {
  constructor(private prisma: PrismaClient) {}
  async create(createMenuDto: CreateMenuDto) {
    const { parent, ...dto } = createMenuDto;
    return this.prisma.menu.create({
      data: {
        ...dto,
        parentId: parent,
      },
    });
  }

  async findAll() {
    const resultdata = await this.prisma.menu.findMany();
    console.log(resultdata);
    return toTrees(null, resultdata);
  }

  findOne(id: number) {
    return this.prisma.menu.findUnique({
      where: { id: id },
    });
  }

  update(id: number, updateMenuDto: UpdateMenuDto) {
    console.log(updateMenuDto);
    return '11';
    // return this.prisma.menu.update({ where: { id: id }, data: updateMenuDto });
  }

  remove(id: number) {
    return this.prisma.menu.delete({ where: { id: id } });
  }
}
